#pragma once
#include <sys/shm.h>
#include <string>
#include <iostream>
#include <sys/ipc.h>
#include <cerrno>
#include <cstring>
#include <sys/shm.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
const std::string pathname = "/home/lyh/proc-commute";
const std::string filename = "fifo";
const int proj_id = 0x11223344;
const int size = 1024;
key_t GetKey()
{
    key_t key = ftok(pathname.c_str(), proj_id);
    if (key < 0)
    {
        std::cout << "errno:" << errno << ",strerrno:" << strerror(errno) << std::endl;
        exit(-1);
    }
    return key;
}

int CreateShmHelper(key_t key, int flag)
{
    int shmid = shmget(key, size, flag);
    if (shmid < 0)
    {
        std::cout << "errno:" << errno << ",strerrno:" << strerror(errno) << std::endl;
        exit(-2);
    }
    return shmid;
}

int CreateShm(key_t key)
{
    return CreateShmHelper(key, IPC_CREAT | IPC_EXCL | 0664);
}

int GetShm(key_t key)
{
    return CreateShmHelper(key, IPC_CREAT | 0664);
}

std::string toHex(int num)
{
    char buffer[1024];
    snprintf(buffer, sizeof(buffer), "0x%x", num);
    return buffer;
}

void makefifo()
{
    int n = mkfifo(filename.c_str(), 0664);
    if (n < 0)
    {
        //std::cout << "errno:" << errno << ",strerrno:" << strerror(errno) << std::endl;
        return;
    }
    std::cout << "创建管道成功!!!" << std::endl;
}